Skip to content

Memory leak in parse_response usage of pydantic#3068

Open
avilaton wants to merge 1 commit intoopenai:mainfrom
avilaton:fix/responses-parse-memory-leak
Open

Memory leak in parse_response usage of pydantic#3068
avilaton wants to merge 1 commit intoopenai:mainfrom
avilaton:fix/responses-parse-memory-leak

Conversation

@avilaton
Copy link
Copy Markdown

@avilaton avilaton commented Apr 7, 2026

  • I understand that this repository is auto-generated and my pull request may not be merged

Changes being requested

Address a memory leak in parse_response detected using the OpenAI client in a webserver context (gunicorn, gevent)

Additional context & links

Problem

client.responses.parse() can trigger sustained memory growth on pydantic >= 2.11 (see #1181).

The issue is that parse_response() used subscripted runtime generic aliases (for example ParsedResponse[T]) when calling construct_type_unchecked(). In pydantic v2, this can cause repeated runtime generic specialization/schema work in a hot path.

Fix

Use the non-subscripted runtime classes in parse_response() when calling construct_type_unchecked():

  • ParsedResponseOutputText[TextFormatT]ParsedResponseOutputText
  • ParsedResponseOutputMessage[TextFormatT]ParsedResponseOutputMessage
  • ParsedResponse[TextFormatT]ParsedResponse

Why this is safe

construct_type_unchecked() constructs models loosely and does not require runtime generic specialization for correctness here. parse_text() still produces the typed parsed payload, and the return type for callers remains ParsedResponse[TextFormatT].

Tests

  • Added a regression test in tests/lib/responses/test_parsing.py that fails if parse_response() routes through _validate_non_model_type.
  • Added an opt-in memory characterization test (OPENAI_RUN_MEMORY_TESTS=1) in tests/lib/responses/test_parsing.py.
  • Ran responses suites against the mock server: 158 passed.

@avilaton avilaton requested a review from a team as a code owner April 7, 2026 18:51
@avilaton avilaton changed the title fix(responses): avoid runtime generic specialization in parse_response Memory leak in parse_response usage of pydantic Apr 7, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant